
import java.lang.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;

public class GererCategorie extends JDialog implements ActionListener {

  private JLabel lCat;
  private JTextField tCat;
  private JButton bOk;
  private JButton bQuit;
  private String oldName;
  private Container contenu;

  public GererCategorie(Categorie c) {
    super((JDialog) null, true);
    this.setLayout(null);
    this.setTitle("Modifier Catégorie");
    this.oldName = c.getNom();
    this.lCat = new JLabel("Nom catégorie: ");
    this.tCat = new JTextField(c.getNom());

    this.bOk = new JButton("Modifier");
    this.bOk.addActionListener(this);

    this.bQuit = new JButton("Annuler");
    this.bQuit.addActionListener(this);

    this.contenu = this.getContentPane();

    this.contenu.add(this.lCat);
    this.contenu.add(this.tCat);
    this.contenu.add(this.bOk);
    this.contenu.add(this.bQuit);

    this.lCat.setBounds(10, 40, 100, 20);
    this.tCat.setBounds(130, 40, 100, 20);
    this.bOk.setBounds(10, 70, 100, 20);
    this.bQuit.setBounds(130, 70, 100, 20);

    this.setBounds(20, 20, 300, 200);
    this.setVisible(true);
    this.setResizable(false);
  }

  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == bOk) {
      try {
        Categorie c = Categorie.getCategorie(this.oldName);
        c.setNom(this.tCat.getText());
        JOptionPane.showMessageDialog(this, "Modification réussie", "Modifier catégorie", JOptionPane.INFORMATION_MESSAGE);
        this.dispose();
      } catch (SQLException et) {
        JOptionPane.showMessageDialog(this, "Modification de la catégorie impossible", "Modifier catégorie", JOptionPane.ERROR_MESSAGE);
      }
    }
    if (e.getSource() == bQuit) {
      this.dispose();
    }
  }
}
